package com.sskj.login.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.login.R;
import com.sskj.login.R2;
import com.sskj.login.bean.LoginBean;
import com.sskj.login.bean.rxbus.AccountBean;
import com.sskj.login.component.DaggerUserDataComponent;
import com.sskj.login.presenter.LoginActivityPresenter;
import com.sskj.login.util.TipLoginUtil;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.LOGIN_LOGIN)//登录
public class LoginActivity extends BaseActivity<LoginActivityPresenter> {
    @BindView(R2.id.tvRegister)
    TextView tvRegister;
    @BindView(R2.id.etAccount)
    EditText etAccount;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.btLogin)
    Button btLogin;
    @BindView(R2.id.tvPwdForget)
    TextView tvPwdForget;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R2.id.tvPhoneLogin)
    TextView tvPhoneLogin;
    @BindView(R2.id.tvEmailLogin)
    TextView tvEmailLogin;
    @BindView(R2.id.llcode)
    LinearLayout llcode;
    @BindView(R2.id.tvCode)
    TextView tvCode;

    private AndroidNextInputs inputs;
    @Inject
    UserViewModel userViewModel;
    private BottomSheetDialog googleCheckDialog;

    private WidgetAccess access;
    private boolean isSee;
    private BottomSheetDialog priceModeSheet;
    private int type = 1;

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity_login;
    }

    @Override
    public LoginActivityPresenter getPresenter() {
        return new LoginActivityPresenter();
    }

    @Override
    protected void initView() {
        RxBus.getDefault().send(RxBusCode.LOGOUT);
        DaggerUserDataComponent.create().inject(this);
        RxBus.getDefault().register(this);
        ClickUtil.click(tvRegister, () -> {
            ARouter.getInstance().build(RConfig.LOGIN_REGISTER).navigation();
        });


        ClickUtil.click(btLogin, () -> {

            inputs.clear();
            inputs
                    .add(access.findEditText(R.id.etAccount), StaticScheme.NotBlank(), SchemeUtil.getEmailPhoneScheme())
                    .add(access.findEditText(R.id.etPwd), StaticScheme.NotBlank(), SchemeUtil.getPwdScheme())
            ;
            if (TextUtils.isEmpty(etAccount.getText().toString())) {
                if (type == 1) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity1_j));
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity2_j));
                }
                return;
            }
            if (type == 1) {
                if (tvCode.getText().toString().equals("86")) {
                    if (inputs.test()) {
                        if (etAccount.getText().toString().trim().length() != 11) {
                            ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                            return;
                        }
                    }
                }
                if (tvCode.getText().toString().equals("60")) {
                    if (etAccount.getText().toString().trim().length() != 9 && etAccount.getText().toString().trim().length() != 10) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("852")) {
                    if (etAccount.getText().toString().trim().length() != 8) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("853")) {
                    if (etAccount.getText().toString().trim().length() != 8) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("65")) {
                    if (etAccount.getText().toString().trim().length() != 8) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("63")) {
                    if (etAccount.getText().toString().trim().length() != 10) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("82")) {
                    if (etAccount.getText().toString().trim().length() != 10 && etAccount.getText().toString().trim().length() != 11) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("81")) {
                    if (etAccount.getText().toString().trim().length() != 10 && etAccount.getText().toString().trim().length() != 11) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("001")) {
                    if (etAccount.getText().toString().trim().length() != 10) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("66")) {
                    if (etAccount.getText().toString().trim().length() != 9 && etAccount.getText().toString().trim().length() != 10) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("886")) {
                    if (etAccount.getText().toString().trim().length() != 9) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
            }
            mPresenter.login(tvCode.getText().toString(), etAccount.getText().toString(), etPwd.getText().toString());
        });

        ClickUtil.click(200, ivPwdToggle, () -> {
            isSee = !isSee;
            etPwd.setTransformationMethod(isSee ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggle.setImageResource(isSee ? R.mipmap.login_icon_show : R.mipmap.login_icon_hide);

        });

        ClickUtil.click(tvPwdForget, () -> {
            ARouter.getInstance().build(RConfig.LOGIN_PWD_FORGET).navigation();
        });

        ClickUtil.click(tvPhoneLogin, () -> {
            tvPhoneLogin.setTextColor(getResources().getColor(R.color.commonWhite));
            tvPhoneLogin.setTextSize(25);
            tvEmailLogin.setTextColor(getResources().getColor(R.color.login_b2b));
            tvEmailLogin.setTextSize(18);
            etAccount.setHint(App.INSTANCE.getString(R.string.login_loginActivity4_j));
            llcode.setVisibility(View.VISIBLE);
            etAccount.setPadding(140, 0, 0, 0);
            type = 1;
        });

        ClickUtil.click(tvEmailLogin, () -> {
            tvEmailLogin.setTextColor(getResources().getColor(R.color.commonWhite));
            tvEmailLogin.setTextSize(25);
            tvPhoneLogin.setTextColor(getResources().getColor(R.color.login_b2b));
            tvPhoneLogin.setTextSize(18);
            etAccount.setHint(App.INSTANCE.getString(R.string.login_loginActivity5_j));
            llcode.setVisibility(View.GONE);
            etAccount.setPadding(0, 0, 0, 0);
            type = 2;
        });

        ClickUtil.click(llcode, () -> {
            if (priceModeSheet == null) {
                priceModeSheet = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, v) -> {
                    priceModeSheet.dismiss();

                    position = position + 1;
                    if (position == 1) {
                        tvCode.setText("60");
                    } else if (position == 2) {
                        tvCode.setText("852");
                    } else if (position == 3) {
                        tvCode.setText("853");
                    } else if (position == 4) {
                        tvCode.setText("65");
                    } else if (position == 5) {
                        tvCode.setText("63");
                    } else if (position == 6) {
                        tvCode.setText("82");
                    } else if (position == 7) {
                        tvCode.setText("81");
                    } else if (position == 8) {
                        tvCode.setText("001");
                    } else if (position == 9) {
                        tvCode.setText("66");
                    } else if (position == 10) {
                        tvCode.setText("886");
                    }
                }, "60", "852", "853", "65", "63", "82", "81", "001", "66", "886");
            }
            priceModeSheet.show();
        });

        inputs = new AndroidNextInputs();
        access = new WidgetAccess(this);
        LiveDataBus.get()
                .with(RxBusCode.REGISTER_ACCOUNT, AccountBean.class)
                .observe(this, accountBean -> {
                    if (etAccount != null && accountBean != null) {
                        if (accountBean.getType() == type) {
                            etAccount.setText(accountBean.getAccount());
                        } else {
                            etAccount.setText("");
                        }
                    } else {
                        etAccount.setText("");
                    }
                });
    }

    public void loginSuccess(LoginBean data) {

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(SPConfig.ACCOUNT, data.getAccount());
        httpHeaders.put(SPConfig.TOKEN, data.getToken());
        SPUtil.put(SPConfig.USER_ACCOUNT, etAccount.getText().toString());
        SPUtil.put(SPConfig.ACCOUNT, data.getAccount());
        SPUtil.put(SPConfig.TOKEN, data.getToken());
        SPUtil.put(SPConfig.MOBILE, etAccount.getText().toString());
        SPUtil.put(SPConfig.GOOGLETYPE, data.getIs_start_google());
        SPUtil.put(SPConfig.LOGIN, true);

        SPUtil.put(SPConfig.CODE, tvCode.getText().toString());

        OkGo.getInstance().addCommonHeaders(new HttpHeaders());
        OkGo.getInstance().getCommonHeaders().clear();

        OkGo.getInstance().addCommonHeaders(httpHeaders);
        userViewModel.update();
        finish();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);

    }

    public void googleCheck(LoginBean loginBean) {

        googleCheckDialog = TipLoginUtil.showGoogleCheckDialog(this, pwd -> {
            mPresenter.checkGoogle(pwd, loginBean);
        });
        googleCheckDialog.show();

    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);

        super.onDestroy();
    }
}
